package com.yellowpage.nsp.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.nsp.data.NspServer;
import com.yellowpage.nsp.service.NspServerService;

public class NspServerAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<NspServer> nspServerList = new ArrayList<NspServer>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private NspServer nspServer;
	private NspServerService nspServerService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_serverId;
		private String query_ip;
		private String query_os;
		private String query_userPassword;
		private String query_logDir;
		private String query_createUser;
		private String query_createTime;
		private String query_lastUpdateUser;
		private String query_lastUpdateTime;
		private String query_note;
		private String query_name;
		private String query_type;
		private String query_status;
		private String query_userName;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspServer = nspServerService.getNspServer(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("serverId", query_serverId);
	    	    queryMap.put("ip", query_ip);
	    	    queryMap.put("os", query_os);
	    	    queryMap.put("userPassword", query_userPassword);
	    	    queryMap.put("logDir", query_logDir);
	    	    queryMap.put("createUser", query_createUser);
	    	    queryMap.put("createTime", query_createTime);
	    	    queryMap.put("lastUpdateUser", query_lastUpdateUser);
	    	    queryMap.put("lastUpdateTime", query_lastUpdateTime);
	    	    queryMap.put("note", query_note);
	    	    queryMap.put("name", query_name);
	    	    queryMap.put("type", query_type);
	    	    queryMap.put("status", query_status);
	    	    queryMap.put("userName", query_userName);
	    	    	
		resultList = nspServerService.queryNspServerByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		nspServerService.saveNspServer(nspServer);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    	    	    if (nspServer.getIp().length() == 0) {
			this.addActionError("** IP地址 不能为空 !");
		}
			    	    	    if (nspServer.getOs().length() == 0) {
			this.addActionError("** 操作系统 不能为空 !");
		}
			    	    	    if (nspServer.getUserPassword().length() == 0) {
			this.addActionError("** 登录用户密码 不能为空 !");
		}
			    	    	    if (nspServer.getLogDir().length() == 0) {
			this.addActionError("** 日志文件目录 不能为空 !");
		}
			    	    	    	    	    	    	    	    	    	    	    if (nspServer.getNote().length() == 0) {
			this.addActionError("** 备注 不能为空 !");
		}
			    	    	    if (nspServer.getName().length() == 0) {
			this.addActionError("** 服务器名称 不能为空 !");
		}
			    	    	    if (nspServer.getType().length() == 0) {
			this.addActionError("** 服务器类型 不能为空 !");
		}
			    	    	    if (nspServer.getStatus().length() == 0) {
			this.addActionError("** 服务器状态 不能为空 !");
		}
			    	    	    if (nspServer.getUserName().length() == 0) {
			this.addActionError("** 登录用户 不能为空 !");
		}
			    	    
		this.setValidatorResult("/nsp/NspServerAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    nspServer = nspServerService.getNspServer(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    nspServerService.updateNspServer(nspServer);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    			    if (nspServer.getIp().length() == 0) {
			this.addActionError("** 服务器ID 不能为空 !");
		}
			    			    if (nspServer.getOs().length() == 0) {
			this.addActionError("** 操作系统  不能为空 !");
		}
			    			    if (nspServer.getUserPassword().length() == 0) {
			this.addActionError("** 登录用户密码 不能为空 !");
		}
			    			    if (nspServer.getLogDir().length() == 0) {
			this.addActionError("** 日志文件目录 不能为空 !");
		}
			    			    			    			    			    			    if (nspServer.getNote().length() == 0) {
			this.addActionError("** 备注 不能为空 !");
		}
			    			    if (nspServer.getName().length() == 0) {
			this.addActionError("** 服务器名称 不能为空 !");
		}
			    			    if (nspServer.getType().length() == 0) {
			this.addActionError("** 服务器类型 不能为空 !");
		}
			    			    if (nspServer.getStatus().length() == 0) {
			this.addActionError("** 服务器状态 不能为空 !");
		}
			    			    if (nspServer.getUserName().length() == 0) {
			this.addActionError("** 登录用户  不能为空 !");
		}
			    		this.setValidatorResult("/nsp/NspServerUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.nspServerService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setNspServer(NspServer nspServer) {
		this.nspServer = nspServer;
	}
	
	public NspServer getNspServer() {
		return nspServer;
	}
	 
	 
	 public void setNspServerService(NspServerService nspServerService) {
		this.nspServerService = nspServerService;
	}
	
	public List<NspServer> getNspServerList() {
		return nspServerList;
	}

	public void setNspServerList(List<NspServer> nspServerList) {
		this.nspServerList = nspServerList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_serverId() {
        return this.query_serverId;
    }
    
    public void setQuery_serverId(String serverId){
        this.query_serverId = serverId;
    }    

        public String getQuery_ip() {
        return this.query_ip;
    }
    
    public void setQuery_ip(String ip){
        this.query_ip = ip;
    }    

        public String getQuery_os() {
        return this.query_os;
    }
    
    public void setQuery_os(String os){
        this.query_os = os;
    }    

        public String getQuery_userPassword() {
        return this.query_userPassword;
    }
    
    public void setQuery_userPassword(String userPassword){
        this.query_userPassword = userPassword;
    }    

        public String getQuery_logDir() {
        return this.query_logDir;
    }
    
    public void setQuery_logDir(String logDir){
        this.query_logDir = logDir;
    }    

        public String getQuery_createUser() {
        return this.query_createUser;
    }
    
    public void setQuery_createUser(String createUser){
        this.query_createUser = createUser;
    }    

        public String getQuery_createTime() {
        return this.query_createTime;
    }
    
    public void setQuery_createTime(String createTime){
        this.query_createTime = createTime;
    }    

        public String getQuery_lastUpdateUser() {
        return this.query_lastUpdateUser;
    }
    
    public void setQuery_lastUpdateUser(String lastUpdateUser){
        this.query_lastUpdateUser = lastUpdateUser;
    }    

        public String getQuery_lastUpdateTime() {
        return this.query_lastUpdateTime;
    }
    
    public void setQuery_lastUpdateTime(String lastUpdateTime){
        this.query_lastUpdateTime = lastUpdateTime;
    }    

        public String getQuery_note() {
        return this.query_note;
    }
    
    public void setQuery_note(String note){
        this.query_note = note;
    }    

        public String getQuery_name() {
        return this.query_name;
    }
    
    public void setQuery_name(String name){
        this.query_name = name;
    }    

        public String getQuery_type() {
        return this.query_type;
    }
    
    public void setQuery_type(String type){
        this.query_type = type;
    }    

        public String getQuery_status() {
        return this.query_status;
    }
    
    public void setQuery_status(String status){
        this.query_status = status;
    }    

        public String getQuery_userName() {
        return this.query_userName;
    }
    
    public void setQuery_userName(String userName){
        this.query_userName = userName;
    }    

    
	
}
